
/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved.
 * Note: 提供的缺省代码仅供参考，可自行根据答题需要进行使用、修改或删除。
 */

import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.HashMap;
import java.util.Scanner;
import java.util.TreeSet;

/**
 * 简易DHCP服务器
 *
 * @author 命题组
 * @since 2020-3-4
 */
public class Main {
    /**
     * main入口由OJ平台调用
     */
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in, StandardCharsets.UTF_8.name());
        DhcpServer dhcpServer = new DhcpServer();
        int operationCnt = cin.nextInt();
        for (int i = 0; i < operationCnt; i++) {
            String[] operation = cin.next().split("=");
            if ("REQUEST".equals(operation[0])) {
                System.out.println(dhcpServer.request(operation[1]));
            } else {
                dhcpServer.release(operation[1]);
            }
        }
        cin.close();
    }

    static class DhcpServer {

        //记录第一轮升序已经分配到那个IP
        private static int distributeIndex=0;

        //记录当前已分配的mac和IP
        private HashMap<String,String> distributes;

        //记录分配历史,（释放和申请时都要进行维护）
        private HashMap<String,String> distributeLog;

        //记录已经释放的IP,升序排列
        private TreeSet<Integer> releases;

        DhcpServer() {
            // 在此补充你的代码
            distributes = new HashMap<>();
            distributeLog = new HashMap<>();
            releases = new TreeSet<>();
        }

        String request(String mac) {

            // 在此补充你的代码
            if(distributes.containsKey(mac)){
                return distributes.get(mac);
            }else if(distributeLog.containsKey(mac)){

                distributes.put(mac,distributeLog.get(mac));
                return distributeLog.get(mac);

            }else if(distributes.size()<=256){
                //初始序列分配
                if(distributeIndex<=255){
                    String ip="192.168.0."+distributeIndex;
                    distributes.put(mac,ip);
                    distributeIndex++;
                    return ip;
                }else{//释放序列分配,
                    String ip="192.168.0."+releases.first();
                    distributes.put(mac,ip);
                    releases.remove(releases.first());

                    //删除历史记录
                    Collection collection = distributeLog.values();
                    collection.remove(ip);

                    return ip;
                }
            }else{
                return "NA";
            }
        }

        void release(String mac) {
            // 在此补充你的代码
            if(distributes.containsKey(mac)){
                //添加到历史记录
                distributeLog.put(mac,distributes.get(mac));
                //维护释放列表
                String[] ips=distributes.get(mac).split("\\.");
                releases.add(Integer.valueOf(Integer.valueOf(ips[3])));
                //删除
                distributes.remove(mac);
            }
        }
    }
}

